package userFriend

import (
    "gitee.com/zwx784533/zwx2018/model"
    "gitee.com/zwx784533/zwx2018/app"
    "time"
)

const TABLE = "t_user_friend"
const DB  = "db_user"

type userFriend struct {
    Uid int `json:"uid"`
    FUid int `json:"f_uid"`
    Mark string `json:"mark"`
    Status int `json:"status"`
    CreateTime int `json:"create_time"`
    UpdateTime int `json:"update_time"`
}

func AddFriend(uid int, fUid int, status int, mark string) error  {
    uf := &userFriend{}
    uf.Uid = uid
    uf.FUid = fUid
    uf.Status = status
    uf.Mark = mark
    uf.CreateTime = int(time.Now().Unix())
    uf.UpdateTime = uf.CreateTime
    err := uf.add()
    return err
}



func (u *userFriend) add()  error  {
    // 选择 db_user
    db, err := model.GetDb(DB)
    if err != nil {
        go app.Error(err)
        return err
    }
    sql := "insert into " + TABLE + " set " +
        "`uid`=?, `f_uid`=?, `status`=?, `mark`=?,`create_time`=?, `update_time`=? " +
        "on duplicate key update `update_time`=?"
    _, err = db.Exec(sql, u.Uid, u.FUid, u.Status,
        u.Mark, u.CreateTime, u.UpdateTime, u.UpdateTime)
    if err != nil {
        go app.Error(err, "sql:", sql)
        return err
    }
    return nil
}

func (u *userFriend) save() error  {
    // 选择 db_user
    db, err := model.GetDb(DB)
    if err != nil {
        go app.Error(err)
        return err
    }
    sql := "update " + TABLE + " set " +
        "`status`=?, `mark`=?,`create_time`=?, `update_time`=? " +
        "where `uid`=? and `f_uid`=?"
    _, err = db.Exec(sql, u.Status,
        u.Mark, u.CreateTime, u.UpdateTime, u.UpdateTime, u.Uid, u.FUid)
    if err != nil {
        go app.Error(err, "sql:", sql)
        return err
    }
    return nil
}

func (u *userFriend) del() error {
    // 选择 db_user
    db, err := model.GetDb(DB)
    if err != nil {
        go app.Error(err)
        return err
    }
    sql := "delete from " + TABLE + " where `uid`=? and `f_uid`=?"
    _, err = db.Exec(sql, u.Status,
        u.Mark, u.CreateTime, u.UpdateTime, u.UpdateTime, u.Uid, u.FUid)
    if err != nil {
        go app.Error(err, "sql:", sql)
        return err
    }
    return nil
}
